<template>
  <div style="margin-top:30px">
    <el-form ref="form" :rules="rules" :model="form" label-width="80px">
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="标题" prop="title" style="margin-right:15px">
              <el-input v-model="form.title" placeholder="请输入新闻标题" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="副标题" prop="subhead" style="margin-right:15px">
              <el-input type="textarea" :rows="3" v-model="form.subhead" placeholder="请输入新闻副标题" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="编辑者" prop="editName" style="margin-right:15px">
              <el-input v-model="form.editName" placeholder="请输入新闻编辑者" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="类别" prop="newsType" style="margin-right:15px">
              <el-select v-model="form.newsType" clearable placeholder="请选择">
                <el-option
                  v-for="(v,k) in dicts.newsType"
                  :key="k"
                  :label="v"
                  :value="k"
                />
              </el-select>
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="图片" prop="imgUrl" style="margin-right:15px">
              <el-upload
                v-model="form.imgUrl"
                list-type="picture-card"
                :action="uploadUrl"
                :show-file-list="false"
                :on-success="handleImgSuccess"
                :before-upload="beforeImgUpload"
                :headers="getAuthorization()"
              >
                <img v-if="form.imgUrl" :src="form.imgUrl" class="el-upload-list__item-thumbnail" width="100%" height="100%">
                <i v-else class="el-icon-plus avatar-uploader-icon" />
              </el-upload>
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="新闻内容" prop="content" style="margin-right:15px">
              <myEditor v-model="form.content" :disabled="false" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="funcCancel">取 消</el-button>
      <el-button type="primary" @click="funcSubmit">确 定</el-button>
    </div>
  </div>
</template>
<script>
import Vue from 'vue'
import 'xe-utils'
import VXETable from 'vxe-table'
import 'vxe-table/lib/index.css'
import request from '@/utils/request'
import myEditor from '@/components/MyEditor'
Vue.use(VXETable)
export default {
  name: 'NewsAdd',
  components: {
    myEditor
  },
  data() {
    return {
      uploadUrl: this.BASE_API_URL + 'ntocc-tps-api/fileUpload',
      dicts: { isRelease: {}, newsType: {}},
      form: {
        imgUrl: '',
        title: '',
        subhead: '',
        content: '',
        editName: '',
        newsType: '',
        ifRelease: '0'
      },
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' },
          { min: 1, max: 100, message: '长度在 1 到 100 个字符', trigger: 'blur' }
        ],
        subhead: [
          { required: true, message: '请输入副标题', trigger: 'blur' },
          { min: 1, max: 300, message: '长度在 1 到 300 个字符', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入内容', trigger: 'blur' },
          { min: 1, max: 24000, message: '长度在 1 到 24000 个字符', trigger: 'blur' }
        ],
        editName: [
          { required: true, message: '请输入编辑者', trigger: 'blur' },

        ],
        newsType: [
          { required: true, message: '请选择类别', trigger: 'blur' }
        ],
        imgUrl: [
          { required: true, message: '请上传图片', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
  },
  methods: {
    // 提交
    funcSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          const rLoading = this.openLoading();
          const data = this.form
          request({ url: '/arpa-basic-api/news', method: 'post', data }).then(response => {
            rLoading.close();
            this.$message({ message: response.msg, type: 'success' })
            this.$emit('fatherAddMethod')
          }).then(() => this.$refs['form'].resetFields())
        } else {
          return false
        }
      })
    },
    // 取消
    funcCancel() {
      this.$emit('fatherCancelMethod', 'add')
    },
    init(dicts) {
      this.dicts = dicts
      this.form = {
        imgUrl: '',
        title: '',
        subhead: '',
        content: '',
        editName: '',
        newsType: '',
        ifRelease: '0'
      }
    },
    handleImgSuccess(res, file) {
      this.$message({ message: res.msg, type: 'success' })
      this.form.imgUrl = res.data
    },
    // 上传验证
    beforeImgUpload(file) {
      const isJPG =
                    file.type === 'image/jpeg' ||
                    file.type === 'image/bmp' ||
                    file.type === 'image/png'
      const isLtnM = file.size / 1024 / 1024 < 10
      if (!isJPG) {
        this.$message.error('上传图片只能是 JPG,BMP,PNG 格式!')
      }
      if (!isLtnM) {
        this.$message.error('上传图片大小不能超过 10MB!')
      }
      return isJPG && isLtnM
    }
  }
}
</script>
<style lang="scss" scoped>
  .dialog-footer {
    margin: 15px 0 15px 0;
    width: 175px;
    float: right;
  }
</style>
